package com.fsnbigdata.domain;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import io.springlets.format.EntityFormat;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.OneToMany;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import java.util.Objects;
import javax.persistence.Entity;
import org.springframework.util.Assert;

/**
 * = Planting
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{introNo}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{introNo}")
public class Planting {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @ManyToOne(fetch = FetchType.EAGER)
    @EntityFormat
    private PlantConservation plantConservation;

    /**
     * 引种号
     */
    private String introNo;

    /**
     * 定植单位
     */
    private String plantingUnit;

    /**
     * 定植日期
     */
    private String plantingDate;

    /**
     * 坐标
     */
    private String coordinate;

    /**
     * 海拔
     */
    private String altitude;

    /**
     * 数量
     */
    private String quantity;

    /**
     * 备注
     */
    private String notes;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "planting", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    private Set<PlantingImage> images = new HashSet<PlantingImage>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof Planting)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((Planting) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return PlantConservation
     */
    public PlantConservation getPlantConservation() {
        return this.plantConservation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param plantConservation
     */
    public void setPlantConservation(PlantConservation plantConservation) {
        this.plantConservation = plantConservation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getIntroNo() {
        return this.introNo;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param introNo
     */
    public void setIntroNo(String introNo) {
        this.introNo = introNo;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getPlantingUnit() {
        return this.plantingUnit;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param plantingUnit
     */
    public void setPlantingUnit(String plantingUnit) {
        this.plantingUnit = plantingUnit;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getPlantingDate() {
        return this.plantingDate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param plantingDate
     */
    public void setPlantingDate(String plantingDate) {
        this.plantingDate = plantingDate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCoordinate() {
        return this.coordinate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param coordinate
     */
    public void setCoordinate(String coordinate) {
        this.coordinate = coordinate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getAltitude() {
        return this.altitude;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param altitude
     */
    public void setAltitude(String altitude) {
        this.altitude = altitude;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getQuantity() {
        return this.quantity;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param quantity
     */
    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getNotes() {
        return this.notes;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param notes
     */
    public void setNotes(String notes) {
        this.notes = notes;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<PlantingImage> getImages() {
        return this.images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param images
     */
    public void setImages(Set<PlantingImage> images) {
        this.images = images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String toString() {
        return "Planting {" + "id='" + id + '\'' + ", version='" + version + '\'' + ", introNo='" + introNo + '\'' + ", plantingUnit='" + plantingUnit + '\'' + ", plantingDate='" + plantingDate + '\'' + ", coordinate='" + coordinate + '\'' + ", altitude='" + altitude + '\'' + ", quantity='" + quantity + '\'' + ", notes='" + notes + '\'' + ", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' + ", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToAdd
     */
    public void addToImages(Iterable<PlantingImage> imagesToAdd) {
        Assert.notNull(imagesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (PlantingImage item : imagesToAdd) {
            this.images.add(item);
            item.setPlanting(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToRemove
     */
    public void removeFromImages(Iterable<PlantingImage> imagesToRemove) {
        Assert.notNull(imagesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (PlantingImage item : imagesToRemove) {
            this.images.remove(item);
            item.setPlanting(null);
        }
    }
}
